/*
 * 描          述:  <描述>
 * 修  改   人:  PengQingyang
 * 修改时间:  2022年5月18日
 * <修改描述:>
 */
package com.tx.component.sendsms.configuration;

import javax.annotation.Resource;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import com.tx.component.communication.starter.CommunicationContextProperties;
import com.tx.component.sendsms.SendSMSConstants;
import com.tx.component.sendsms.dao.SmsTemplateDao;
import com.tx.component.sendsms.dao.impl.SmsTemplateDaoMybatisImpl;
import com.tx.component.sendsms.plugin.aliyun.AliyunSendSMSPlugin;
import com.tx.component.sendsms.plugin.juhe.JuheSendSMSPlugin;
import com.tx.component.sendsms.service.SmsTemplateService;
import com.tx.core.exceptions.util.AssertUtils;
import com.tx.core.mybatis.support.MyBatisDaoSupport;
import com.tx.core.starter.component.ComponentConstants;

/**
 * 发送短信容器持久层配置器<br/>
 * <功能详细描述>
 * 
 * @author  PengQingyang
 * @version  [版本号, 2022年5月18日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Configuration
public class SendSMSContextPersisterConfiguration {
    
    /**
     * mybatis持久层逻辑实现<br/>
     * <功能详细描述>
     * @author  Administrator
     * @version  [版本号, 2019年5月2日]
     * @see  [相关类/方法]
     * @since  [产品/模块版本]
     */
    @Configuration
    @ConditionalOnProperty(prefix = ComponentConstants.PERSISTER_PROPERTIES_PREFIX, value = "type", havingValue = "mybatis")
    @ConditionalOnBean(name = { "tx.component.myBatisDaoSupport" })
    public static class MybatisAuthContextPersisterConfiguration
            implements InitializingBean {
        
        /** mybatis属性 */
        @Resource(name = "tx.component.myBatisDaoSupport")
        private MyBatisDaoSupport myBatisDaoSupport;
        
        /** 事务管理器 */
        @SuppressWarnings("unused")
        private TransactionTemplate transactionTemplate;
        
        private CommunicationContextProperties properties;
        
        /** <默认构造函数> */
        public MybatisAuthContextPersisterConfiguration(
                PlatformTransactionManager transactionManager,
                CommunicationContextProperties properties) {
            this.transactionTemplate = new TransactionTemplate(
                    transactionManager);
            this.properties = properties;
        }
        
        /**
         * @throws Exception
         */
        @Override
        public void afterPropertiesSet() throws Exception {
            AssertUtils.notNull(this.myBatisDaoSupport,
                    "myBatisDaoSupport is null.");
        }
        
        /**
         * 权限类型项持久层<br/>
         * <功能详细描述>
         * @return [参数说明]
         * 
         * @return AuthTypeItemDao [返回类型说明]
         * @exception throws [异常类型] [异常说明]
         * @see [类、类#方法、类#成员]
         */
        @Bean(name = SendSMSConstants.BEAN_NAME_SMS_TEMPLATE_DAO)
        public SmsTemplateDao smsTemplateDao() {
            SmsTemplateDaoMybatisImpl dao = new SmsTemplateDaoMybatisImpl();
            dao.setMyBatisDaoSupport(this.myBatisDaoSupport);
            return dao;
        }
        
        /**
         * 权限类型项业务层<br/>
         * <功能详细描述>
         * @return [参数说明]
         * 
         * @return AuthTypeItemService [返回类型说明]
         * @exception throws [异常类型] [异常说明]
         * @see [类、类#方法、类#成员]
         */
        @Bean(SendSMSConstants.BEAN_NAME_SMS_TEMPLATE_SERVICE)
        public SmsTemplateService smsTemplateService(
                SmsTemplateDao smsTemplateDao) {
            SmsTemplateService service = new SmsTemplateService(
                    this.properties.getSendsms().getEnumBasePackages(),
                    this.properties.getSendsms().getConfigLocation(),
                    smsTemplateDao, this.transactionTemplate);
            return service;
        }
    }
    
    /**
     * github登陆插件<br/>
     * <功能详细描述>
     * @return [参数说明]
     * 
     * @return GHLoginPlugin [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    @ConditionalOnProperty(prefix = "tx.component.communication.sendsms", value = "aliyun", havingValue = "true")
    @Bean("sendSMS.aliyunPlugin")
    public AliyunSendSMSPlugin aliyunSendSMSPlugin() {
        AliyunSendSMSPlugin plugin = new AliyunSendSMSPlugin();
        return plugin;
    }
    
    /**
     * github登陆插件<br/>
     * <功能详细描述>
     * @return [参数说明]
     * 
     * @return GELoginPlugin [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    @ConditionalOnProperty(prefix = "tx.component.communication.sendsms", value = "juhe", havingValue = "true")
    @Bean("sendSMS.juhePlugin")
    public JuheSendSMSPlugin juheSendSMSPlugin() {
        JuheSendSMSPlugin plugin = new JuheSendSMSPlugin();
        return plugin;
    }
}
